//
//  MacAddressTool.m
//  DevSpoof
//
//  Created by huang on 2018/11/7.
//  Copyright © 2018年 wdys. All rights reserved.
//

#import "MacAddressHelper.h"
#import <ifaddrs.h>
#import <netinet/in.h>
#import <arpa/inet.h>
#import <sys/sysctl.h>
#import <net/if.h>
#import <net/if_dl.h>
#import "CommandLine.h"
@implementation MacAddressHelper
+ (NSString *)randomMacAddress{
    return [CommandLine command:@"/usr/bin/openssl" argvs:@[@"rand",@"-hex",@"6"]].pipe(@"/usr/bin/sed",@"s/\\(..\\)/\\1:/g; s/.$//").read;
}
+ (NSString *)getDeviceNameWithHardWareName:(NSString *)name{
    // Ethernet | Wi-Fi
    //networksetup -listallhardwareports | grep Wi-Fi -A 1 | grep 'en' | cut -d' ' -f2
    CommandLine *command = [CommandLine command:@"/usr/sbin/networksetup" argvs:@"-listallhardwareports"]
    .pipe(@"/usr/bin/grep", @[name,@"-A",@"1"])
    .pipe(@"/usr/bin/grep", @[@"en"])
    .pipe(@"/usr/bin/cut", @[@"-d ",@"-f2"]);
    return [[command read] stringByReplacingOccurrencesOfString:@"\n" withString:@""];
}
+ (NSString *)getDeviceMacAddress:(NSString *)deviceName{
    int                 mib[6];
    size_t              len;
    char                *buf;
    unsigned char       *ptr;
    struct if_msghdr    *ifm;
    struct sockaddr_dl  *sdl;
    
    mib[0] = CTL_NET;
    mib[1] = AF_ROUTE;
    mib[2] = 0;
    mib[3] = AF_LINK;
    mib[4] = NET_RT_IFLIST;
    
    if ((mib[5] = if_nametoindex(deviceName.UTF8String)) == 0) {
        return NULL;
    }
    if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0) {
        return NULL;
    }
    if ((buf = malloc(len)) == NULL) {
        return NULL;
    }
    if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
        printf("Error: sysctl, take 2");
        return NULL;
    }
    ifm = (struct if_msghdr *)buf;
    sdl = (struct sockaddr_dl *)(ifm + 1);
    ptr = (unsigned char *)LLADDR(sdl);
    NSString *outstring = [NSString stringWithFormat:@"%02x:%02x:%02x:%02x:%02x:%02x", *ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4), *(ptr+5)];
    // release pointer
    free(buf);
    return [outstring lowercaseString];
}
+ (NSString *)originalEthernetMacAddress{
    NSString *originalMac = [CommandLine command:@"/usr/sbin/networksetup" argvs:@[@"-listallhardwareports",@"| grep 'Port: Ethernet' -A 2 | grep Address | cut -d' ' -f3"]].read;
    originalMac = [originalMac stringByReplacingOccurrencesOfString:@"\n" withString:@""];
    return originalMac;
}
+ (NSString *)originalWifiAddress{
    NSString *originalMac = [CommandLine command:@"/usr/sbin/networksetup" argvs:@[@"-listallhardwareports",@"| grep Wi-Fi -A 2 | grep Address | cut -d' ' -f3"]].read;
    originalMac = [originalMac stringByReplacingOccurrencesOfString:@"\n" withString:@""];
    return originalMac;
}
+ (NSString *)currentWifiMacAdresss{
    NSString *deviceName = [self getDeviceNameWithHardWareName:@"Wi-Fi"];
    return [self getDeviceMacAddress:deviceName];
}
+ (NSString *)currentEthernetMacAdresss{
    NSString *deviceName = [self getDeviceNameWithHardWareName:@"Ethernet"];
    return [self getDeviceMacAddress:deviceName];
}

@end
